package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.bean.CurrentThreadLocalUserInfo;
import cn.dansj.exception.InterfaceResponseException;
import cn.dansj.common.utils.transfer.AssertUtils;
import cn.dansj.common.utils.transfer.Verification;
import cn.dansj.handler.InterfaceHandler;
import cn.dansj.utils.project.ProjectUtils;

import java.util.Map;

@SuppressWarnings("all")
@MarkAsApi
public class ChangeUsername extends InterfaceHandler {
    @Override
    public Object execute(Map<String, Object> paramsMap) {
        final String sequence = CurrentThreadLocalUserInfo.getCurrentThreadLocalUserInfo().getCacheToken().getSequence();
        final Object nickname = paramsMap.get("nickname");
        AssertUtils.state(Verification.checkNotNull(nickname), new InterfaceResponseException(500, "用户名不能为空"));
        int update = jdbcTemplate.update("update x_user_info set nick_name=? where sequence=?", nickname.toString(), sequence);
        AssertUtils.state(update == 1, new InterfaceResponseException(500, "用户名更新失败"));
        return ProjectUtils.encryptByPrivateKey(nickname.toString());
    }
}